﻿using System;
using System.Collections.Generic;
using System.Linq;

using Microsoft.AspNet.Membership.OpenAuth;
using System.DirectoryServices.AccountManagement;
using System.Web.UI.WebControls;

public partial class ChangePassword : System.Web.UI.Page
{
    protected string SuccessMessage
    {
        get;
        private set;
    }

    protected bool CanRemoveExternalLogins
    {
        get;
        private set;
    }

    protected void Page_Load()
    {
        if (!IsPostBack)
        {
            // Determine the sections to render
            setPassword.Visible = true;
        }
        
    }

    protected void setPassword_Click(object sender, EventArgs e)
    {
        if (IsValid)
        {
            String domain = System.Configuration.ConfigurationSettings.AppSettings["Domain"].ToString();

            try
            {
                PrincipalContext ctx = new PrincipalContext(ContextType.Domain,domain, User.Identity.Name, oldpassword.Text);

                if (!User.Identity.Name.Contains("@"))
                {
                    UserPrincipal adUser = UserPrincipal.FindByIdentity(ctx, IdentityType.SamAccountName, User.Identity.Name);
                    adUser.ChangePassword(oldpassword.Text, password.Text);
                }
                else
                {
                    UserPrincipal adUser = UserPrincipal.FindByIdentity(ctx, IdentityType.UserPrincipalName, User.Identity.Name);
                    adUser.ChangePassword(oldpassword.Text, password.Text);
                }

                SuccessMessage = "Your password has been changed.";
                successMessage.Visible = !String.IsNullOrEmpty(SuccessMessage);
            }
            catch (Exception ex)
            {
                SuccessMessage = ex.InnerException.Message.ToString();
                successMessage.Visible = !String.IsNullOrEmpty(SuccessMessage);
            }
        }
    }

    
    public IEnumerable<OpenAuthAccountData> GetExternalLogins()
    {
        var accounts = OpenAuth.GetAccountsForUser(User.Identity.Name);
        CanRemoveExternalLogins = CanRemoveExternalLogins || accounts.Count() > 1;
        return accounts;
    }

    public void RemoveExternalLogin(string providerName, string providerUserId)
    {
        var m = OpenAuth.DeleteAccount(User.Identity.Name, providerName, providerUserId)
            ? "?m=RemoveLoginSuccess"
            : String.Empty;
        Response.Redirect("~/Account/Manage.aspx" + m);
    }
    

    protected static string ConvertToDisplayDateTime(DateTime? utcDateTime)
    {
        // You can change this method to convert the UTC date time into the desired display
        // offset and format. Here we're converting it to the server timezone and formatting
        // as a short date and a long time string, using the current thread culture.
        return utcDateTime.HasValue ? utcDateTime.Value.ToLocalTime().ToString("G") : "[never]";
    }
}